//
//  TempMainActionExt.swift
//  ANOASTORE
//
//  Created by 邱仙凯 on 2024/1/30.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit

extension ConfigMenuForm {
    func configAction() {
        for i in 0..<self.mainHandler.count {
            let temp = self.mainHandler[i].temp!
            
            
            if isShowJ(temp,auCombine: self.tempAuditParamC,exType: self.excuteType) {
                var theitem:KSFormBaseItem?
                theitem = (self.mainHandler[i].item as? KSFormHItem).isSome ? (self.mainHandler[i].item as? KSFormHItem) : (self.mainHandler[i].item as? KSFormVItem)
                
                switch temp.field_type {
                case TemplateType.baselovtree.rawValue:
                    theitem?.actionClosure = {
                        TempHelper.shared.temp_selectPigFarmer(currentNm: USERINFO.zOrgNm.unwrap) {  [unowned self] (one) in
                            for item in temp.lovBackFillArr {
                                let pre = item.preChar(char: ":").unwrap.camelName
                                let suf = item.sufChar(char: ":").unwrap
                                let vvv = any2Str(getValueByKey(obj: one, key: pre))
                                self.main[suf] = vvv
                                KSLog("键：\(pre)  值：\(vvv)  给：\(suf)")
                            }
                            self.configTempDataSource()
                            self.continueUrl(onexx: temp)
                        }
                        
                    }
                case TemplateType.companylovtree.rawValue:
                    theitem?.actionClosure = {
                        
                        openOrgSelectUtil {  nodes in
                            for item in temp.lovBackFillArr {
                                let pre = item.preChar(char: ":").unwrap.camelName
                                let suf = item.sufChar(char: ":").unwrap
                                let vvv = any2Str(getValueByKey(obj: nodes.first!, key: pre))
                                self.main[suf] = vvv
                            }
                            self.configTempDataSource()
                            self.continueUrl(onexx: temp)
                        }
//                        KSOrgSelectUtil.initWithNav(type: .single,backClosure: { nodes in
//                            
//                            for item in temp.lovBackFillArr {
//                                let pre = item.preChar(char: ":").unwrap.camelName
//                                let suf = item.sufChar(char: ":").unwrap
//                                let vvv = any2Str(getValueByKey(obj: nodes.first!, key: pre))
//                                self.main[suf] = vvv
//                            }
//                            self.configTempDataSource()
//                            self.continueUrl(onexx: temp)
//                        }) { vc in
//                            let topViewController = RouterUtils.currentTopViewController()
//                            topViewController!.present_FullScreen(to: vc)
//                        }
                        
                    }
                case TemplateType.dormlovtree.rawValue:
                    theitem?.actionClosure = {
                        
                        openDormSelectUtil { node in
                            
                            for item in temp.lovBackFillArr {
                                let pre = item.preChar(char: ":").unwrap
                                let suf = item.sufChar(char: ":").unwrap
                                let vvv = any2Str(getValueByKey(obj: node, key: pre))
                                self.main[suf] = vvv
                            }
                            self.configTempDataSource()
                            self.continueUrl(onexx: temp)
                        }
                        
                    }
                case TemplateType.lov.rawValue,
                    TemplateType.editlov.rawValue,
                    TemplateType.btnevent_lov.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
                            self.showLov(onexx: onexx, indexPath: nil)
                        }
                        
                    }
                case TemplateType.arealov.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        self.showAreaLov(onexx: onexx, indexPath: nil)
                    }
                case TemplateType.tree.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
                            self.showTree(onexx: onexx, indexPath: nil)
                        }
                        
                    }
                case TemplateType.btnevent.rawValue,
                    TemplateType.btnevent_exec.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
                            self.continueUrl(onexx: onexx)
                        }
                    }
                    // 发票直接使用lov 该功能暂不使用
                case TemplateType.invoice.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
//                            self.continueUrl(onexx: onexx)
                        }
                    }
                case TemplateType.coordinate.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
                            
                            self.go2MapVC(onexx: onexx, indexPath: nil)
                            
                        }
                        
                    }
                case TemplateType.gangedlov.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        // 联动
                        self.showLiandong( oneD: onexx, indexPath: nil)
                        
                        
                    }
                case TemplateType.input.rawValue,
                    TemplateType.number.rawValue,
                    TemplateType.integer.rawValue,
                    TemplateType.number2.rawValue:
                    theitem?.textEndEditClo = { str in
                        self.main[self.mainHandler[i].temp!.field_nm.unwrap] = str
                        // 执行主表的影响
                        self.setEffect()
                        self.computerAction()
                    }
                case TemplateType.date.rawValue:
                    theitem?.actionClosure = {
                        
                        TempHelper.shared.showTempDate(vc:self,onexx: temp) { str in
                            
                            self.main[self.mainHandler[i].temp!.field_nm.unwrap] = str
                            self.configTempDataSource()
                            
                        }
                        
                        
                    }
                case TemplateType.datetime.rawValue:
                    theitem?.actionClosure = {
                        self.dateTimePick.resultClosure = { str in
                            
                            
                            
                            
                            
                        
                           let timeString = convertTo24HourFormat(from: str)
                         
                             self.main[self.mainHandler[i].temp!.field_nm.unwrap] = timeString
                            self.configTempDataSource()
                            
                        }
                        self.dateTimePick.present()
                        
                    }
                case TemplateType.localSelect.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        self.showXLSelect(onexx: onexx, indexPath: nil)
                    }
         
                    
                case TemplateType.month.rawValue:
                    theitem?.actionClosure = {
                        let onexx = self.mainHandler[i].temp!
                        if self.dependenceVali(onexx: onexx, indexPath: nil) {
                            let view = KSSelectMonthView()
                            view.fetchData = { month in
                                
                                
                                self.main[self.mainHandler[i].temp!.field_nm.unwrap] = month
                                // 有后续接口就调用，不用去刷页面，因为后续接口会刷页面
                                if onexx.call_port_url.unwrap != "" {
                                    self.continueUrl(onexx: onexx)
                                } else {
                                    self.configTempDataSource()
                                    
                                }
                                
                                
                                
                                
                                
                            }
                            view.show()
                        }
                    }
                case TemplateType.radio.rawValue:
                    (self.mainHandler[i].item as! KSSwitchItem).switchValueChangeClo = { flag in
                        self.main[self.mainHandler[i].temp!.field_nm.unwrap] =  flag ? "1" : "0"
                    }
             
                    
                    
                case  TemplateType.slidingblock.rawValue :
                    (self.mainHandler[i].item as! KSBlockItem).blockValueChangeClo = { segData in
                        let onexx = self.mainHandler[i].temp!
                        let sBack = onexx.selectDataBack!
                        self.main[sBack.idBack.unwrap] = segData.id
                        self.main[sBack.nmBack.unwrap] = segData.nm
                    }
                    
                default:
                    break
                }
                
            }
        }
        
    }
}
// 手动转换24小时制显示
func convertTo24HourFormat(from timeString: String) -> String {
   var result = timeString
   KSLog(timeString)
   result = result.replacingOccurrences(of: "上午1:", with: "01:")
    result = result.replacingOccurrences(of: "上午2:", with: "02:")
    result = result.replacingOccurrences(of: "上午3:", with: "03:")
    result = result.replacingOccurrences(of: "上午4:", with: "04:")
    result = result.replacingOccurrences(of: "上午5:", with: "05:")
    result = result.replacingOccurrences(of: "上午6:", with: "06:")
    result = result.replacingOccurrences(of: "上午7:", with: "07:")
    result = result.replacingOccurrences(of: "上午8:", with: "08:")
    result = result.replacingOccurrences(of: "上午9:", with: "09:")
    result = result.replacingOccurrences(of: "上午10:", with: "10:")
    result = result.replacingOccurrences(of: "上午11:", with: "11:")
    result = result.replacingOccurrences(of: "上午12:", with: "00:")
    result = result.replacingOccurrences(of: "下午1:", with: "13:")
    result = result.replacingOccurrences(of: "下午2:", with: "14:")
    result = result.replacingOccurrences(of: "下午3:", with: "15:")
    result = result.replacingOccurrences(of: "下午4:", with: "16:")
    result = result.replacingOccurrences(of: "下午5:", with: "17:")
    result = result.replacingOccurrences(of: "下午6:", with: "18:")
    result = result.replacingOccurrences(of: "下午7:", with: "19:")
    result = result.replacingOccurrences(of: "下午8:", with: "20:")
    result = result.replacingOccurrences(of: "下午9:", with: "21:")
    result = result.replacingOccurrences(of: "下午10:", with: "22:")
    result = result.replacingOccurrences(of: "下午11:", with: "23:")
    result = result.replacingOccurrences(of: "下午12:", with: "12:")
  return result
}
 
